//
//  PicShow.m
//  JiuyaoDriver
//
//  Created by 大橙子 on 2018/11/22.
//  Copyright © 2018 Tomous. All rights reserved.
//

#import "PicShow.h"
@interface PicShow()<UIScrollViewDelegate>
@property (nonatomic, strong)UIImageView *oldImageView;

@property (nonatomic, strong)UIImageView *picImageView;
@property (nonatomic, strong)UIScrollView *scrollView;

@end
@implementation PicShow
+ (PicShow *)showImage:(UIImageView *)imageView url:(NSString *__nullable)url image:(UIImage *__nullable)image
{
    PicShow *picBrower = [[PicShow alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) url:url image:image imageView:imageView];
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:picBrower];
    return picBrower;
}

- (instancetype)initWithFrame:(CGRect)frame url:(NSString *__nullable)url image:(UIImage *__nullable)image imageView:(UIImageView *)imageView
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor blackColor];
        
        _oldImageView = imageView;
        
        _scrollView = [[UIScrollView alloc]init];
        _scrollView.delegate = self;
        _scrollView.scrollEnabled = YES;
        _scrollView.maximumZoomScale = 3;
        [self addSubview:_scrollView];
        
        _picImageView = [[UIImageView alloc]init];
        if (Str_IsEmpty(url)) {
            _picImageView.image = image;
        }else{
            [_picImageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:IMAGENAME(@"")];
        }
        [_scrollView addSubview:_picImageView];
        
        CGRect frame = [imageView.superview convertRect:imageView.frame toView:[UIApplication sharedApplication].keyWindow];
        _scrollView.frame = frame;
        _picImageView.frame = _scrollView.bounds;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick:)];
        [self addGestureRecognizer:tap];
        
        [self animation];
    }
    return self;
}
- (void)animation
{
    __weak typeof(self) weakself = self;
    [UIView animateWithDuration:0.5 animations:^{
        weakself.scrollView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
        weakself.picImageView.frame = weakself.scrollView.bounds;
        weakself.picImageView.contentMode = UIViewContentModeScaleAspectFit;
        
    }];
}
- (void)tapClick:(UITapGestureRecognizer *)tap
{
    CGRect frame = [_oldImageView.superview convertRect:_oldImageView.frame toView:[UIApplication sharedApplication].keyWindow];
    
    [UIView animateWithDuration:0.2 animations:^{
        _scrollView.frame = frame;
        _picImageView.frame = _scrollView.bounds;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        
    }];
}
#pragma mark - UIScrollViewDelegate,告诉scrollview要缩放的是哪个子控件
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return self.picImageView;
}
#pragma mark - 等比例放大，让放大的图片保持在scrollView的中央
- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
    CGFloat offsetX = (self.scrollView.bounds.size.width > self.scrollView.contentSize.width)?(self.scrollView.bounds.size.width - self.scrollView.contentSize.width) *0.5 : 0.0;
    CGFloat offsetY = (self.scrollView.bounds.size.height > self.scrollView.contentSize.height)?
    (self.scrollView.bounds.size.height - self.scrollView.contentSize.height) *0.5 : 0.0;
    self.picImageView.center =CGPointMake(self.scrollView.contentSize.width *0.5 + offsetX,self.scrollView.contentSize.height *0.5 + offsetY);
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
